#include "Display.h"

Display::Display(wxWindow* parent) : wxPanel(parent, wxID_ANY)
{
	img.Create(10, 10);
	Bind(wxEVT_PAINT, &Display::OnPaint, this, wxID_ANY);
	Bind(wxEVT_SIZE, &Display::OnResize, this, wxID_ANY);
}

void Display::ShowImage(char* _imageData, int _height, int _width)
{

	char* imgD = (char*)malloc(_height * _width*3);
	for (int i = 0; i < _height * _width; i++)
	{
		imgD[i * 3] = _imageData[i];
		imgD[i * 3 + 1] = _imageData[i];
		imgD[i * 3 + 2] = _imageData[i];
	}
	img.SetData((unsigned char*)imgD, _width, _height,false);
	if (clearBackground || height != _height || width != _width)
	{
		height = _height;
		width = _width;
		clearBackground = false;
		Refresh(true);
	}
	else
	{
		Refresh(false);
	}
}

void Display::OnResize(wxSizeEvent& event)
{
	clearBackground = true;
}

void Display::OnPaint(wxPaintEvent& event)
{
	wxPaintDC paintDC(this);

	wxSize winSize = GetClientSize();


	if (height - winSize.y > width - winSize.x)
	{
		paintDC.DrawBitmap(img.Scale((double)winSize.y / (double)height * (double)width, winSize.y, wxIMAGE_QUALITY_BOX_AVERAGE), 0, 0);
	}
	else
	{
		paintDC.DrawBitmap(img.Scale(winSize.x, (double)winSize.x / (double)width * (double)height,wxIMAGE_QUALITY_BOX_AVERAGE), 0, 0);
	}
}